from django.shortcuts import render
from django.http import HttpResponse
import re
#dashboard登录中间件
class DashLoginMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        #One-time configuration and initialization.

    def __call__(self, request):

        #检测当前的请求是否已经登录,如果已经登录,则放行,如果未登录,则跳转到登录页
        #获取当前用户的请求路径  
        urllist = ['/dashboard/index/','/dashboard/dologin/','/dashboard/login_page/','/dashboard/verifycode/']
        #判断是否进入了后台,并且不是进入登录页面
        if re.match('/dashboard/',request.path) and request.path not in urllist:

            #检测session中是否存在 user_info的数据记录
            if request.session.get('user_info','') == '':
                #如果在session没有记录,则证明没有登录,跳转到登录页面
                return HttpResponse('<script>alert("请先登录");location.href="/dashboard/login_page/";</script>')


        #网页用中间件规则
        weburllist = ['/web/create_order/','/web/payme/']
        # print('path:'+request.path)
        if request.path in weburllist:
            #检测session中是否存在 adminlogin的数据记录
            if request.session.get('web_user_info','') == '':
                return HttpResponse('<script>alert("请先登录");location.href="/web/login/";</script>')

        response = self.get_response(request)

        return response